﻿/*
 * BFind - A tool for finding football matches for trading on Betfair
 * Copyright 2009, 2010 Magnus Olsson <magnus72(at)gmail.com>
 * 
 * This file is part of BFind.
 * 
 * BFind is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BFind is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BFind.  If not, see <http://www.gnu.org/licenses/>
*/

using System;
using System.Collections.Generic;

namespace betfairfind.Rules
{
    class Under25Firstgoal: BaseRule
    {
        double UNDER25_MIN_BACK_ODDS = 1.90;
        double UNDER25_MAX_BACK_ODDS= 10;
        double UNDER25_MIN_MATCHED= 500;
        double FG0_10_MIN_BACK_ODDS= 5;
        double FG0_10_MAX_BACK_ODDS = 100;
        double FG_MIN_MATCHED = 500;




        public Under25Firstgoal(Dictionary<string, string> config, List<MyMarket> inplaymarkets, Dictionary<long, MarketPricesCompressed> marketprices) : base()
         {

             MarketsUsedInside = new string[] { "over/under 2.5 goals", "first goal odds"};

             try
             {

                 UNDER25_MIN_BACK_ODDS = Convert.ToDouble(config["Rule.Under25Firstgoal.UNDER25_MIN_BACK_ODDS"]);
                 UNDER25_MAX_BACK_ODDS = Convert.ToDouble(config["Rule.Under25Firstgoal.UNDER25_MAX_BACK_ODDS"]);
                 UNDER25_MIN_MATCHED = Convert.ToDouble(config["Rule.Under25Firstgoal.UNDER25_MIN_MATCHED"]);
                 FG0_10_MIN_BACK_ODDS = Convert.ToDouble(config["Rule.Under25Firstgoal.FG0_10_MIN_BACK_ODDS"]);
                 FG0_10_MAX_BACK_ODDS = Convert.ToDouble(config["Rule.Under25Firstgoal.FG0_10_MAX_BACK_ODDS"]);
                 FG_MIN_MATCHED = Convert.ToDouble(config["Rule.Under25Firstgoal.FG_MIN_MATCHED"]);

             }
             catch (Exception e)
             {
                 Logger.LogError(e.ToString(), true, true);
             }

             string criteria = @"" + UNDER25_MIN_BACK_ODDS + "/" +
                 UNDER25_MAX_BACK_ODDS + "/" +
                 UNDER25_MIN_MATCHED + "/" +
                 FG0_10_MIN_BACK_ODDS + "/" +
                 FG0_10_MAX_BACK_ODDS + "/" +
                 FG_MIN_MATCHED;

             Logger.Log("============================================================================", true, true);
             Logger.Log("Matches qualified for Under 2.5 and First goal (0-10min) (" + criteria + ")", true, true);
             Logger.Log("----------------------------------------------------------------------------", true, true);

             if (!CheckRequiredMarkets()) { return; } 

             string[] games = FindSoccerGames.GetMatches();

             int selectionCount = 0;
             foreach (var game in games)
             {
                 try
                 {
                    MyMarket market_ou = FindSoccerGames.GetMarket(game, "Over/Under 2.5 goals");
                    MarketPricesCompressed mpc_ou = marketprices[market_ou.marketId];

                    MyMarket market_fg = FindSoccerGames.GetMarket(game, "first goal odds");
                    MarketPricesCompressed mpc_fg = marketprices[market_fg.marketId];

                         
                    double under25odds = mpc_ou.GetBestBackOdds(0);
                    double fg010odds = mpc_fg.GetBestBackOdds(0);
             
                    double matched_ou = mpc_ou.TotalMatched;
                    double matched_fg = mpc_fg.TotalMatched;


                    if (under25odds >= UNDER25_MIN_BACK_ODDS && under25odds <= UNDER25_MAX_BACK_ODDS && matched_ou >= UNDER25_MIN_MATCHED
                        && fg010odds >= FG0_10_MIN_BACK_ODDS && fg010odds <= FG0_10_MAX_BACK_ODDS && matched_fg >= FG_MIN_MATCHED)
                    {

                    Logger.Log(market_ou.marketTime + ", " + BF.convertPathToGame(game) + ", u2.5: " + under25odds + ", o/u 2.5 matched: " + matched_ou + config["CURRENCY_SIGN"] + ", fg 0-10: " + fg010odds + ", fg matched: " + matched_fg +  config["CURRENCY_SIGN"], true, true);
                    selectionCount++;

                    }
                    

                 }
                 catch (Exception ex)
                 {

                     Logger.LogError("Error in game " + game + Environment.NewLine + ex.ToString(), true, false);

                 }

             }
             if (selectionCount == 0)
             {
                 Logger.Log("No matches", true, true);
             }
         }
    }
}


